package com.premize.grco.dataaccess.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

import com.premize.grco.dataaccess.sessionFactory.HibernateSessionFactory;
import com.premize.grco.movil.GrcoProducto;


/**
 * A data access object (DAO) providing persistence and search support for
 * GrcoProducto entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 *
 * @author Zathura Code Generator http://code.google.com/p/zathura
 *
 */
public class GrcoProductoDAO implements IGrcoProductoDAO {
    private static final Log log = LogFactory.getLog(GrcoProductoDAO.class);

    // property constants
    //public static final Long  CANTIDADDISPONIBLE = "cantidadDisponible";
    public static final String CANTIDADDISPONIBLE = "cantidadDisponible";

    //public static final Long  CANTIDADMINIMA = "cantidadMinima";
    public static final String CANTIDADMINIMA = "cantidadMinima";

    //public static final String  CODIGOPRODUCTO = "codigoProducto";
    public static final String CODIGOPRODUCTO = "codigoProducto";

    //public static final Long  ESTADO = "estado";
    public static final String ESTADO = "estado";

    //public static final String  NOMBRE = "nombre";
    public static final String NOMBRE = "nombre";

    //public static final String  PROMOCION = "promocion";
    public static final String PROMOCION = "promocion";

    //public static final Double  VALOR = "valor";
    public static final String VALOR = "valor";

    private Session getSession() {
        return HibernateSessionFactory.getSession();
    }

    /**
    *
    * @param Instance
    *            GrcoProducto Instance to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(GrcoProducto instance) {
        log.debug("saving GrcoProducto instance");

        try {
            getSession().save(instance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    /**
    * @param Instance
    *            GrcoProducto Instance to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(GrcoProducto instance) {
        log.debug("deleting GrcoProducto instance");

        try {
            getSession().delete(instance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    /**
    *
    * @param Instance
    *            GrcoProducto Instance to update
    * @throws RuntimeException
    *             when the operation fails
    */
    public void update(GrcoProducto instance) {
        log.debug("updating GrcoProducto instance");

        try {
            getSession().update(instance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public GrcoProducto findById(String id) {
        log.debug("finding GrcoProducto instance with id: " + id);

        try {
            GrcoProducto instance = (GrcoProducto) getSession()
                                                       .get(GrcoProducto.class,
                    id);

            return instance;
        } catch (RuntimeException re) {
            log.error("finding GrcoProducto failed", re);
            throw re;
        }
    }

    public List<GrcoProducto> findByExample(GrcoProducto instance) {
        log.debug("finding GrcoProducto instance by example");

        try {
            List results = getSession().createCriteria("GrcoProducto")
                               .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " +
                results.size());

            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    /**
    * Find all  GrcoProducto entities with a specific property value.
    *
    * @param value
    *            the property value to match
    * @param propertyName
    *            the property to search in the instance
    * @return List< GrcoProducto> found by query
        */
    public List<GrcoProducto> findByProperty(String propertyName, Object value) {
        log.debug("finding GrcoProducto instance with property: " +
            propertyName + ", value: " + value);

        try {
            String queryString = "from GrcoProducto as model where model." +
                propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<GrcoProducto> findByCantidadDisponible(
        Object cantidadDisponible) {
        return findByProperty(CANTIDADDISPONIBLE, cantidadDisponible);
    }

    public List<GrcoProducto> findByCantidadMinima(Object cantidadMinima) {
        return findByProperty(CANTIDADMINIMA, cantidadMinima);
    }

    public List<GrcoProducto> findByCodigoProducto(Object codigoProducto) {
        return findByProperty(CODIGOPRODUCTO, codigoProducto);
    }

    public List<GrcoProducto> findByEstado(Object estado) {
        return findByProperty(ESTADO, estado);
    }

    public List<GrcoProducto> findByNombre(Object nombre) {
        return findByProperty(NOMBRE, nombre);
    }

    public List<GrcoProducto> findByPromocion(Object promocion) {
        return findByProperty(PROMOCION, promocion);
    }

    public List<GrcoProducto> findByValor(Object valor) {
        return findByProperty(VALOR, valor);
    }

    /**
    * Find all GrcoProducto entities.
    *
    * @return List<GrcoProducto> all GrcoProducto instances
    */
    public List<GrcoProducto> findAll() {
        log.debug("finding all GrcoProducto instances");

        try {
            String queryString = "from GrcoProducto";
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List<GrcoProducto> findByCriteria(String whereCondition) {
        log.debug("finding GrcoProducto " + whereCondition);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from GrcoProducto model " +
                where;
            Query query = getSession().createQuery(queryString);
            List<GrcoProducto> entitiesList = query.list();

            return entitiesList;
        } catch (RuntimeException re) {
            log.error("find By Criteria in GrcoProducto failed", re);
            throw re;
        }
    }

    public List<GrcoProducto> findPageGrcoProducto(String sortColumnName,
        boolean sortAscending, int startRow, int maxResults) {
        log.debug("finding GrcoProducto findPageGrcoProducto");

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from GrcoProducto model order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from GrcoProducto model";

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    public Long findTotalNumberGrcoProducto() {
        log.debug("finding GrcoProducto count");

        try {
            String queryString = "select count(*) from GrcoProducto model";
            Query queryObject = getSession().createQuery(queryString);

            return (Long) queryObject.list().get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }
}
